package com.axter.app.test.testpics;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.axter.libs.photo.ImageLoaderCache;
import com.axter.libs.photo.choosephoto.ChoosePhotoActivity;
import com.axter.libs.photo.choosephoto.ChoosePhotoIntent;
import com.axter.libs.photo.display.SimplePhotoDisplay;
import com.axter.libs.photo.previewphoto.PreviewPhotoActivity;
import com.axter.libs.photo.previewphoto.PreviewPhotoBaseActivity;
import com.axter.libs.photo.previewphoto.PreviewPhotoIntent;
import com.axter.libs.photo.utils.ListMediaUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private Activity instance;
    private MenuListView lv_menus;
    private ListView listview;
    private List<String> print = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;

        setContentView(R.layout.activity_main);
        init(getApplicationContext(), getCacheDir().getAbsolutePath());

        lv_menus = (MenuListView) findViewById(R.id.lv_menus);
        listview = (ListView) findViewById(R.id.listview);
        listview.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, print));

        lv_menus.add("选择", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                new ChoosePhotoIntent(instance)
                        .setMaxSize(10)
                        .setMaxImageSize(9)
                        .setMaxVideoSize(1)
                        .setSingle(false)
                        .setShowGif(true)
                        .setShowVideo(true)
                        .setShowCamera(true)
                        .startActivity(instance, 101);
            }
        });
        lv_menus.add("查看", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                Intent intent = new Intent(instance, PreviewPhotoActivity.class);
                intent.putStringArrayListExtra(PreviewPhotoActivity.EXTRA_IMAGE_PATHS, getPics());
                intent.putExtra(PreviewPhotoActivity.EXTRA_POSITION, 0);
                intent.putExtra(PreviewPhotoActivity.EXTRA_TYPE, PreviewPhotoActivity.EXTRA_TYPE_PREVIEW);
                startActivity(intent);
            }
        });
        lv_menus.add("测试取数据速度", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        long begin = System.currentTimeMillis();
                        new ListMediaUtils(true, true, true, null).getData(MainActivity.this.getApplicationContext());
                        long end = System.currentTimeMillis();
                        print.add(String.valueOf(end - begin));
                        listview.post(new Runnable() {
                            @Override
                            public void run() {
                                ((BaseAdapter) listview.getAdapter()).notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });

        lv_menus.add("查看", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                new PreviewPhotoIntent(instance, PreviewPhotoBaseActivity.EXTRA_TYPE_PREVIEW)
                        .setImages(getPics())
                        .startActivity(instance, 102);
            }
        });

        lv_menus.add("删除", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                new PreviewPhotoIntent(instance, PreviewPhotoBaseActivity.EXTRA_TYPE_DEL)
                        .setImages(getPics())
                        .startActivity(instance, 102);
            }
        });

        lv_menus.add("选择", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                new PreviewPhotoIntent(instance, PreviewPhotoBaseActivity.EXTRA_TYPE_CHECK)
                        .setImages(getPics())
                        .startActivity(instance, 102);
            }
        });

        lv_menus.notifyDataSetChanged();
    }

    private ArrayList<String> getPics() {
        ArrayList<String> pics = new ArrayList<>();
        pics.add("http://s1.dwstatic.com/group1/M00/FB/52/43775872e49eadcbc53edb4209655de4.gif");
        pics.add("http://img5.imgtn.bdimg.com/it/u=2805319622,2331480379&fm=21&gp=0.jpg");
        pics.add("http://g.hiphotos.baidu.com/zhidao/pic/item/37d12f2eb9389b50a9794db98335e5dde6116e53.jpg");
        pics.add("http://c.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=4f582c74b4fb43161a4a727e15946a15/72f082025aafa40f39ce8262ad64034f78f01900.jpg");
        pics.add("http://h.hiphotos.baidu.com/zhidao/wh%3D600%2C800/sign=63f8974c03e93901565785384bdc78df/6c224f4a20a4462316d68a0e9a22720e0cf3d7b9.jpg");
        pics.add("http://s1.dwstatic.com/group1/M00/4B/BD/8f2d4be8d109b72117277171b506d3fd.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/00/06/6585fe6fb02ee84fa68bdaeb7ab77157.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/1D/1C/8bfe1ffdae21700fdb2828e59e2a2a5a.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/0F/B3/a61727ac93a8dddd91d70d8f42b55603.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/90/62/0eb6e84b7c80d6676f7d8e02b2730fc9.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/01/54/a20add4815877115cc542cb9f435c892.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/EA/DF/f188233ec9530eb1ed7d3defb2e3ba4e.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/0E/21/1dbac571349c08751d1dfd90e3882fa7.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/2D/16/1082a11048b869d0fcf1407b07a8919e.gif");
        pics.add("http://s1.dwstatic.com/group1/M00/91/70/5a76fbb27af712b35e4a7126f383a2f4.gif");
        return pics;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 101) {
                if (data != null && data.getExtras() != null) {
                    print.clear();
                    if (data.getExtras().containsKey(ChoosePhotoActivity.EXTRA_OUT_IMAGES)) {
                        List<String> images = data.getExtras().getStringArrayList(ChoosePhotoActivity.EXTRA_OUT_IMAGES);
                        for (String str : images) {
                            print.add(str);
                        }
                    }
                    if (data.getExtras().containsKey(ChoosePhotoActivity.EXTRA_OUT_VIdEOS)) {
                        List<String> videos = data.getExtras().getStringArrayList(ChoosePhotoActivity.EXTRA_OUT_VIdEOS);
                        for (String str : videos) {
                            print.add(str);
                        }
                    }
                    ((BaseAdapter) listview.getAdapter()).notifyDataSetChanged();
                }
            }
        }
    }

    public static void init(Context context, String cacheDir) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.diskCache(new UnlimitedDiskCache(new File(cacheDir)));//自定义缓存路径
        ImageLoader.getInstance().init(config.build());
    }
}
